using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using Musicman.Business;
using Musicman.Common.Entities;
using System.Collections.Generic;
using Musicman.Common;


namespace Musicman.SAL
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    
    [WebService(Namespace="http://vpankovic.musicman.com/webservices")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class MusicmanMediaService : System.Web.Services.WebService
    {
        public AuthHeader Authentication;
        public bool Authenticate(AuthHeader header)
        {
            long? res = UserManager.Instance.AuthenticateUser(header.userName, header.password);
            header.personId = res;
            return res != null;
        }

        [WebMethod]
        [SoapHeader("Authentication", Direction = SoapHeaderDirection.Out)]
        public bool LoggIn(string emaiAddress, string password)
        {
            User user = UserManager.Instance.LoggInUser(emaiAddress, password, LoginType.Client);
            if (user != null)
            {
                this.Authentication = new AuthHeader();
                this.Authentication.userName = emaiAddress;
                this.Authentication.password = password;
                this.Authentication.personId = user.Personid;
            }
            return user != null;
        }

        [WebMethod]
        [SoapHeader("Authentication", Direction = SoapHeaderDirection.InOut)]
        public User GetUserInfo()
        {
            User user = null;
            if (Authenticate(Authentication))
            {
                user = UserManager.Instance.GetUser((long)Authentication.personId, true);
            }
            return user;
        }

        [WebMethod]
        [SoapHeader("Authentication", Direction = SoapHeaderDirection.InOut)]
        public byte[] GetProfilePicture()
        {
            byte[] profilePicture = null;
            if (Authenticate(Authentication))
            {
                profilePicture = UserManager.Instance.LoadProfilePicture((long)Authentication.personId);
            }            
            return profilePicture;
        }

        [WebMethod]
        [SoapHeader("Authentication", Direction = SoapHeaderDirection.InOut)]
        public List<SearchJob> LoadSearchJobs()
        {
            List<SearchJob> jobs = null;
            if (Authenticate(Authentication))
            {
                jobs = SearchJobManager.Instance.LoadAllSearchJobs();
            }     
            return jobs;
        }

        [WebMethod]
        [SoapHeader("Authentication", Direction = SoapHeaderDirection.InOut)]
        public List<SearchFile> SearchFiles(string searchType, long jobId, string keywords, int top, long minSize)
        {
            List<SearchFile> files = null;
            if (!Authenticate(Authentication))
            {
                return files;
            }
            if (searchType == "search")
            {
                files = SearchFileManager.Instance.LoadSearchFiles(jobId, keywords, top, minSize);
            }
            else if (searchType == "topdownloads")
            {
                files = SearchFileManager.Instance.LoadTopDownwloadesFiles(jobId, keywords, top, minSize);
            }
            else if (searchType == "toprated")
            {
                files = SearchFileManager.Instance.LoadTopRatedFiles(jobId, keywords, top, minSize);
            }
            else if (searchType == "latest")
            {
                files = SearchFileManager.Instance.LoadLatestFiles(jobId, keywords, top, minSize);
            }
            else if (searchType == "random")
            {
                files = SearchFileManager.Instance.LoadRandomFiles(jobId, keywords, top, minSize);
            }
            return files;
        }

        [WebMethod]
        [SoapHeader("Authentication", Direction = SoapHeaderDirection.InOut)]
        public List<SearchFile> LoadPendingDownloads()
        {
            List<SearchFile> files = null;
            if (!Authenticate(Authentication))
            {
                return files;
            }
            long personid = (long)Authentication.personId;
            files = SearchFileManager.Instance.LoadPendingDownloads(personid);
            return files;
        }


        [WebMethod]
        [SoapHeader("Authentication", Direction = SoapHeaderDirection.InOut)]
        public bool UpdateUserProfile()
        {
            throw new NotImplementedException();
            /*
            bool res = DataManager.Instance.SaveUser(Authentication.user);
            return res;
            */
        }
    }


    public class AuthHeader : SoapHeader
    {
        public string userName;
        public string password;
        public long? personId;
    }
}
